import os
import sys
import time

from selenium.webdriver.common.by import By

from Base.base_action import BaseAction


class JumpTaobao(BaseAction):
    # 通用按钮
    swipe_loc = (500, 1100, 500, 500)
    go_look = (By.ID, "org.c2h4.afei.beauty:id/ll_go_look")  # 前往查看按钮
    product_loc = (By.ID, "org.c2h4.afei.beauty:id/iv_product")  # 产品按钮
    app_package = "org.c2h4.afei.beauty"
    app_activity = ".splash.SplashActivity"
    # 查产品相关按钮
    search_pro = (By.XPATH, "//*[contains(@text, '查产品')]")  # 查产品
    input_text = (By.ID, "org.c2h4.afei.beauty:id/et_container")  # 搜索输入框
    search_bt = (994, 2118)  # 手机上的搜索按钮位置，需要优化
    search_word = "雅诗兰黛"  # 随便查找什么
    # 护肤榜相关按钮
    spr_btn = (By.XPATH, "//*[contains(@text, '护肤榜')]")  # 护肤榜
    # 美妆榜相关按钮
    makeup_btn = (By.XPATH, "//*[contains(@text, '美妆榜')]")  # 美妆榜
    # 品牌库相关按钮
    brand_lib = (By.XPATH, "//*[contains(@text, '品牌库')]")  # 品牌库
    themes = (By.ID, "org.c2h4.afei.beauty:id/iv_banner")  # 品牌主题馆
    enter_brand = (By.ID, "org.c2h4.afei.beauty:id/ll_go_brand")  # 进入品牌按钮
    shop_name = (By.ID, "org.c2h4.afei.beauty:id/tv_shop_name")  # 店铺名位置
    # 福利情报局相关按钮
    welfare = (By.XPATH, "//*[contains(@text, '福利情报局')]")  # 福利情报局
    discount_buy = (By.XPATH, "//*[contains(@text, '领券购买')]")  # 领券购买
    welfare_swipe_loc = (500, 1500, 500, 500)

    # 应用权限允许
    def private_allowed(self):

        els = self.driver.find_elements_by_class_name('android.widget.Button')
        if els:
            for el in els:
                if el.text == '允许':
                    self.driver.find_element_by_android_uiautomator('new UiSelector().text("允许")').click()
                elif el.text == '本次运行允许':
                    self.driver.find_element_by_android_uiautomator('new UiSelector().text("本次运行允许")').click()
                elif el.text == '仅在使用中允许':
                    self.driver.find_element_by_android_uiautomator('new UiSelector().text("仅在使用中允许")').click()
                elif el.text == '始终允许':
                    self.driver.find_element_by_android_uiautomator('new UiSelector().text("始终允许")').click()
                elif el.text == '确定':
                    self.driver.find_element_by_android_uiautomator('new UiSelector().text("确定")').click()
        time.sleep(1)

    # 从查产品跳转淘宝
    def search_product_jump(self):
        self.click_element(self.search_pro)
        self.click_element(self.input_text)
        self.input_keys(self.input_text, self.search_word)
        w, h = self.get_window_size()
        self.tab(x=w - 30, y=h - 30)
        self.click_element(self.product_loc)
        time.sleep(2)
        self.swipe(*self.swipe_loc)
        self.click_element(self.go_look)
        try:
            self.private_allowed()
        except:
            pass
        file_name = sys._getframe().f_code.co_name
        time_stamp = str(time.time()).split(".")[0]
        time.sleep(5)
        self.screen_shot(r"D:\app_auto_test\ScreenShot" + os.sep + file_name + time_stamp + ".png")

    # 从护肤榜跳转
    def skin_pro_rank_jump(self):
        self.click_element(self.spr_btn)
        self.tab(ele=self.find_elements(self.product_loc)[0])
        time.sleep(2)
        self.swipe(*self.swipe_loc)
        self.click_element(self.go_look)
        file_name = sys._getframe().f_code.co_name
        time_stamp = str(time.time()).split(".")[0]
        time.sleep(5)
        self.screen_shot(r"D:\app_auto_test\ScreenShot" + os.sep + file_name + time_stamp + ".png")

    # 从美妆榜跳转
    def makeup_rank_jump(self):
        self.click_element(self.makeup_btn)
        self.tab(ele=self.find_elements(self.product_loc)[0])
        time.sleep(2)
        self.swipe(*self.swipe_loc)
        self.click_element(self.go_look)
        file_name = sys._getframe().f_code.co_name
        time_stamp = str(time.time()).split(".")[0]
        time.sleep(5)
        self.screen_shot(r"D:\app_auto_test\ScreenShot" + os.sep + file_name + time_stamp + ".png")

    # 从品牌库跳转
    def brand_library_jump(self):
        self.click_element(self.brand_lib)
        self.tab(ele=self.find_elements(self.themes)[0])
        self.tab(ele=self.find_elements(self.enter_brand)[0])
        time.sleep(10)
        self.tab(ele=self.find_elements(self.shop_name)[1])
        file_name = sys._getframe().f_code.co_name
        time_stamp = str(time.time()).split(".")[0]
        time.sleep(5)
        self.screen_shot(r"D:\app_auto_test\ScreenShot" + os.sep + file_name + time_stamp + ".png")

    # 从福利情报局跳转
    def welfare_intelligence_bureau_jump(self):
        self.click_element(self.welfare)
        while True:
            try:
                buy = self.find_element(self.discount_buy)
                if buy:
                    buy.click()
                    file_name = sys._getframe().f_code.co_name
                    time_stamp = str(time.time()).split(".")[0]
                    time.sleep(5)
                    self.screen_shot(r"D:\app_auto_test\ScreenShot" + os.sep + file_name + time_stamp + ".png")
                    break
            except:
                self.swipe(*self.welfare_swipe_loc)



